package com.zshan.clinic.mini.util;


import com.fasterxml.jackson.core.type.TypeReference;
import com.zshan.clinic.admin.entity.medical.exec.base.RxExtExecInfo;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.util.http.HttpUtil;
import com.zshan.clinic.common.util.json.JsonUtil;
import com.zshan.clinic.mini.entity.SessionKey;
import lombok.extern.slf4j.Slf4j;


/**
 * 公众平台通用接口工具类
 *
 */
@Slf4j
public class WeixinUtil {


	public static final String JSCODE_2_SESSION = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";


	/**
	 * 获取sessionKey
	 * @param code
	 * @return
	 */
	public static SessionKey getSessionKey(String appId, String secret, String code) {
		try{
			String requestUrl = JSCODE_2_SESSION.replace("APPID", appId).replace("SECRET", secret).replace("JSCODE",code);
			HttpUtil.Resp resp = HttpUtil.get(requestUrl);
			if(!resp.isOk()){
				throw new BusinessFailException("登录失败，请稍后重试");
			}
			String body = resp.getBody();
			log.info("调用微信获取SessionKey，响应信息:{}",body);
			return JsonUtil.json2Obj(body,new TypeReference<SessionKey>() {});
		}catch (BusinessFailException e){
			log.error("调用微信获取SessionKey",e);
			throw e;
		}catch (Exception e){
			log.error("调用微信获取SessionKey",e);
		}
		return null;
	}
}
